import 'dart:async';

import 'package:bloc_usage/bloc/BlocProvider.dart';
import 'package:bloc_usage/bloc/HomeDrawerBloc.dart';
import 'package:bloc_usage/model/HomeDrawerModel.dart';
import 'package:flutter/material.dart';

class DrawerItem {
  String title;
  IconData icon;
  DrawerItem(this.title, this.icon);
}

class HomeDrawer extends StatefulWidget {
  final List<DrawerItem> items = [
    DrawerItem("设置", Icons.settings),
    DrawerItem("主题", Icons.style),
    DrawerItem("整理", Icons.sort),
  ];

  @override
  State<StatefulWidget> createState() => HomeDrawerState();
}

class HomeDrawerState extends State<HomeDrawer> {
  Timer timer;
  int count = 0;

  @override
  Widget build(BuildContext context) {
    HomeDrawerBloc bloc = BlocProvider.of<HomeDrawerBloc>(context);
    return StreamBuilder<HomeDrawerModel>(
      stream: bloc.homeDrawerStream,
      builder: (BuildContext context,
              AsyncSnapshot<HomeDrawerModel> snapshot) =>
          Column(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Padding(
                child: Center(
                  child: CircleAvatar(
                    child: Image.network(
                      "https://avatars1.githubusercontent.com/u/15081583?v=4",
                      scale: 4,
                      fit: BoxFit.contain,
                    ),
                    radius: 30,
                  ),
                ),
                padding: EdgeInsets.fromLTRB(0, 60, 0, 35),
              ),
              StreamBuilder<int>(
                stream: bloc.countStream,
                initialData: 0,
                builder: (BuildContext context, AsyncSnapshot<int> snapShot) =>
                    Center(
                      child: Text("${snapShot.data}"),
                    ),
              ),
              ListView.builder(
                scrollDirection: Axis.vertical,
                shrinkWrap: true,
                itemCount: widget.items.length,
                itemBuilder: (BuildContext context, int index) {
                  DrawerItem item = widget.items[index];
                  return ListTile(
                      leading: Icon(item.icon), title: Text(item.title));
                },
              )
            ],
          ),
    );
  }
}
